package com.example.watermark;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: IntervalJoin
 * Package: com.example.watermark
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-25
 * Time: 14:47
 */

//间隔联结
public class IntervalJoin {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        final SingleOutputStreamOperator<Tuple2<String, Integer>> s1 =
                env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        String[] s = value.split(" ");

                        return Tuple2.of(s[0], Integer.valueOf(s[1]));
                    }
                }).assignTimestampsAndWatermarks(WatermarkStrategy.
                        <Tuple2<String, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((val, ts) -> val.f1 * 1000L));


        SingleOutputStreamOperator<Tuple2<String, Integer>> s2 =
                env.socketTextStream("hadoop102", 8888)
                        .map(new MapFunction<String, Tuple2<String, Integer>>() {
                            @Override
                            public Tuple2<String, Integer> map(String value) throws Exception {
                                String[] s = value.split(" ");

                                return Tuple2.of(s[0], Integer.valueOf(s[1]));
                            }
                        }).assignTimestampsAndWatermarks(WatermarkStrategy.
                        <Tuple2<String, Integer>>forMonotonousTimestamps()
                        //单位毫秒
                        .withTimestampAssigner((val, ts) -> val.f1 * 1000L));

        //在keyBy之后调用
        s1.keyBy(0)
                .intervalJoin(s2.keyBy(0))
                .between(Time.seconds(-2), Time.seconds(2))
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String>() {
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple2<String,
                            Integer> right, Context ctx, Collector<String> out) throws Exception {
                        out.collect(left + "<-------->" + right);
                    }
                }).print();


        env.execute();


    }
}
